import numpy as np
a= np.array([1,2,4,5,6,7,8,9,10]) #1d array
a
a = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]]) #2d array
a
a = np.arange(1,100,2) #array like range func
a
np.zeros(5) # array filled with zeros in 1D martix
np.zeros((5,5)) #array folled with zero in 2d matrix
np.ones(5) # array filled with ones in 1D martix
np.ones((3,2)) # array filled with ones in 2D martix
np.linspace(1,10,5) # values that are spaced linearly in a specified interval:
arr = np.array([2, 1, 5, 3, 7, 4, 6, 8])
np.sort(arr) #quik sort in ascending order
np.argsort(arr) #which is an indirect sort along a specified axis
arr = np.array([2, 1, 5, 3, 7, 4, 6, 8])
np.searchsorted(arr,5) #find elements in a sorted array
np.partition(arr,2) #it is a partial sort.
In simple words, np.partition(arr, 2) will split the array arr into two parts, one with elements less than or equal to the element at index 2, and the other with elements greater than or equal to the element at index 2. The resulting array will have the element at index 2 as the pivot.
a=np.array([1,2,3,4,5])
b=np.array([6,7,8,9,10])
np.concatenate((a,b)) #You can concatenate them with np.concatenate()
x = np.array([[1, 2], [3, 4]])
y = np.array([[5, 6]])
np.concatenate((x,y),axis=0) #You can concatenate them on index too
np.identity(4)
#creates an identity matrix of size 4x4.
np.identity(4) is a function in the NumPy library that creates an identity matrix of size 4x4. An identity matrix is a square matrix that has ones along the main diagonal (top-left to bottom-right) and zeroes everywhere else.
np.random.rand(3,2) #To get any random matrix
np.random.randint(0,10) #random integer it gives random integer between 0 to 10
a = np.arange(50)
reshaped = a.reshape(5,10) #it will reshapes the array a into a new shape
reshaped
So, the above code will create a 1-D array of shape (50,) with elements from 0 to 49, and then it will reshape it into a 2-D array of shape (5,10) with 5 rows and 10 columns. Each row will have 10 elements, so the first row will be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], the second row will be [10, 11, 12, 13, 14, 15, 16, 17, 18, 19], and so on.
a = np.arange(20)
a
a.max() # to get max value from array
a.min() # to get min value from array
a.mean() # to get mean of array
array_example = np.array([[[0, 1, 2, 3],[4, 5, 6, 7]],[[0, 1, 2, 3],[4, 5, 6, 7]],[[0 ,1 ,2, 3],[4, 5, 6, 7]]])
array_example
[4, 5, 6, 7]],
[[0, 1, 2, 3],
[4, 5, 6, 7]],
[[0, 1, 2, 3],
[4, 5, 6, 7]]])
array_example.ndim #To find the number of dimensions of the array
array_example.size #To find the total number of elements in the array
array_example.shape #And to find the shape of your array
Reshape an array
a = np.arange(6)
a
a.reshape(3,2)
#in np.reshape, you can specify a few optional parameters:-
np.reshape(a,newshape=(2,3))
np.reshape(a,newshape=(1,6),order='C')
a is the array to be reshaped.
newshape is the new shape you want. You can specify an integer or a tuple of integers. If you specify an integer, the result will be an array of that length. The shape should be compatible with the original shape.
order: C means to read/write the elements using C-like index order, F means to read/write the elements using Fortran-like index order, A means to read/write the elements in Fortran-like index order if a is Fortran contiguous in memory, C-like order otherwise. (This is an optional parameter and doesn’t need to be specified.)
x= np.array([1,2,5,6,7,8,10,3]) #Indexing / Slicing
x[4]
x[0:2]
x[1:]
x[-3:]
p=x
p[:]=50 # to change all value to original array not a copy
p
x # ex
a = np.array([[1 , 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
(a[a>5])
data = (a>=5)
data
divisible_by = a[a%2==0]
divisible_by
not_divisible_by = a[a%2!=0]
not_divisible_by
a =np.array([1,2,3,4,5,6,7,8,9])
c= a[(a>2)&(a<7)]
c
c= (a>5)|(a==5)
c
x = np.arange(1, 25).reshape(2, 12)
x
l = np.array([[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15]])
l[0][1] # 0 is the index of raw and 1 is the index of column
l[1] #it's returning the 1st index from the raws
# advance
l[:2,1:]
l[:0,2:]